<template>
	<view :class="[primarySize()]">
		<view class="page-foot bg-white">
			<view class="ptb10 plr30" @click="bindAddJob">
				<view class="btn3">+新增发布</view>
			</view>
		</view>
		<view class="container">
			<view class="page-head flex-box">
				<view class="tab_item flex-grow-1" :class="query.status==1?'active':''" @click="bindStatusChange(1)">招聘中
				</view>
				<view class="tab_item flex-grow-1" :class="query.status==0?'active':''" @click="bindStatusChange(0)">
					暂停招聘</view>
				<view class="tab_item flex-grow-1" :class="query.status==2?'active':''" @click="bindStatusChange(2)">
					审核状态</view>
			</view>
			<view class="pt88 plr30 pb30">
				<!-- <multi-select class="" :items="jobCategories" @change="bindJobCategoryChange" id_key="id"
					name_key="category_name" children_key="childlist" max_level="2" :value="query.job_category_id">
					<view class="picker_nav flex-box">
						<view class="flex-grow-1 fs28 pr30" :class="[!job_category_name?'cola':'col3']">
							{{!job_category_name?'请选择':job_category_name}}
						</view>
						<image src="https://admin.mosizp.cn/static/public/arrow-b2.png" mode="widthFix" class="ico17"></image>
					</view>
				</multi-select> -->
				<view class="pt10">
					<view class="item" v-for="(job,jobIndex) in jobs" :key="jobIndex">
						<view class="flex-box">
							<view class="flex-grow-1 fwb fs36 col3 pr30 m-ellipsis">{{job.job_name}}</view>
							<view class="fs24 cola5">{{formatDatetime(job.createtime,'yyyy-mm-dd')}}</view>
						</view>
						<view class="flex-box mt10">
							<view class="flex-grow-1 pr30">
								<view class="fwb fs32 fwb col8 lh44">{{job.salary_text}}</view>
								<view class="pt10">
									<view class="label">{{job.city.name}}{{job.district?'-'+job.district.name:''}}
									</view>
									<view class="label">{{job.education_level.name}}</view>
									<view class="label">{{job.work_experience.name}}</view>
								</view>
								<view class="mt20 fs24 cola5">应聘简历:
									<text class="col8">{{job.apply_count}}</text>份 | 浏览量:
									<text class="col8">{{job.view_count}}</text>人
								</view>
							</view>
							
							<view v-if="job.status==1">
								<!-- <view class="bt1">置顶</view> -->
								<view class="bt1 mt20" @click="bindRefreshJob(job.id)">刷新</view>
							</view>
						</view>
						<view class="pt20 fs28 red" v-if="job.status==2">正在审核中...请耐心等待</view>
						<view class="pt20 fs28 red" v-else-if="job.status==3">审核拒绝：{{job.job_info.failed_reason}}</view>
						<view class="mt30 pt25 bt flex-box">

							<view class="bt2" @click="bindDelJob(job.id,jobIndex)" v-if="job.status==0||job.status==3">
								删除</view>
							<view class="flex-grow-1"></view>
							<view class="bt3" v-if="job.status==0||job.status==3"
								@click="bindSwitchOnJob(job.id,jobIndex)">再发布</view>
							<view class="bt3" v-if="job.status==1" @click="bindSwitchOffJob(job.id,jobIndex,true)">下架
							</view>
							<!-- <view class="bt3" v-if="job.status==2" @click="bindSwitchOffJob(job.id,jobIndex,false)">下架</view> -->
							<view class="bt3 ml20" v-if="job.status==0||job.status==3" @click="bindEditJob(job.id)">编辑
							</view>
						</view>
					</view>
				</view>

			</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				
				jobCategories: [],
				job_category_name: '',
				query: {
					type: 0,
					status: 1,
					job_category_id: ''
				},
				jobs: [],
				nindex: 0
			}
		},
		onReady() {
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.primaryColor()
			})
		},
		onLoad() {
			
			let page = this;
			
			this.$util.getJobCategories(2, false).then(items => {
				this.jobCategories = items;
			})
			this.refresh();
			uni.$on(app.globalData.Events.JobCreate, this.eventOnJobCreate, this);
			uni.$on(app.globalData.Events.JobUpdate, this.eventOnJobUpdate, this);
		},
		onUnload() {
			uni.$off(app.globalData.Events.JobCreate, this.eventOnJobCreate, this);
			uni.$off(app.globalData.Events.JobUpdate, this.eventOnJobUpdate, this);
		},
		methods: {
			chooseNav(index) {
				this.nindex = index
			},
			bindJobCategoryChange(event) {
				let items = event.items;
				this.job_category_name = items.map(item => item.category_name).join(' - ');
				let selectedOption = items[1];
				this.query.job_category_id = selectedOption.id;
				this.$forceUpdate();
				this.refresh()
			},
			//状态
			bindStatusChange(status) {
				this.query.status = status;
				this.refresh();
			},
			refresh: function() {
				this.jobs = [];
				this.$core.get({
					url: '/xilujob.job_manage/index',
					data: this.query,
					success: ret => {
						this.jobs = ret.data.jobs;
					}
				});
			},
			bindAddJob() {
				this.$core.post({
					url: 'xilujob.job_manage/action_msg',
					data: {},
					success: ret => {
						//到面试邀请页面
						uni.navigateTo({
							url: '/pages/company/publish_full',
						});
					},
					fail: ret => {
						uni.showModal({
							title: "提示",
							content: ret.msg,
							success(res) {
								if (res.confirm) {
									uni.navigateTo({
										url: '/pages/company/taocan',
									})
								}
							}
						})
						return false;
					}
				});
			},
			bindEditJob(jobId) {
				uni.navigateTo({
					url: '/pages/company/publish_full?id=' + jobId
				});
			},
			//刷新
			bindRefreshJob(jobId) {
				this.$core.post({
					url: 'xilujob.job_manage/refresh_job',
					data: {
						id: jobId
					},
					success: ret => {
						uni.showToast({
							title: '刷新成功',
							icon: 'none'
						});
					}
				});
			},
			//上架
			bindSwitchOnJob(jobId, index) {
				let page = this;
				uni.showModal({
					title: '提示',
					content: '是否确认发布职位？',
					success: res => {
						if (res.confirm) {
							page.$core.post({
								url: '/xilujob.job_manage/switch_on/id/' + jobId,
								success: ret => {
									let status = ret.data.status;
									if (page.query.status != status) {
										page.query.status = status;
										page.refresh();
									} else {
										page.jobs[index].status = status;
										page.$forceUpdate();
									}
								}
							})
						}
					}
				});
			},
			//下架
			bindSwitchOffJob: function(jobId, jobIndex, off_job) {
				let page = this;
				uni.showModal({
					title: '提示',
					content: off_job ? '是否下架招聘职位？' : '是否取消发布审核？',
					success: res => {
						if (res.confirm) {
							this.$core.post({
								url: '/xilujob.job_manage/switch_off/id/' + jobId,
								success: ret => {
									page.jobs.splice(jobIndex, 1);
									page.$forceUpdate();
								}
							})
						}
					}
				});
			},
			//删除
			bindDelJob(jobId, index) {
				let page = this;
				uni.showModal({
					title: '提示',
					content: '是否确认删除职位？',
					success: res => {
						if (res.confirm) {
							page.$core.post({
								url: '/xilujob.job_manage/del/id/' + jobId,
								success: ret => {
									page.jobs.splice(index, 1);
									page.$forceUpdate();
								}
							});
						}
					}
				});
			},
			eventOnJobCreate: function(job) {
				let status = job.status;
				if (this.query.status != status) {
					this.query.status = status;
					this.refresh();
				} else {
					this.jobs.splice(0, 0, job);
					this.$forceUpdate();
				}
			},
			eventOnJobUpdate: function(job) {
				for (let i = 0; i < this.jobs.length; i++) {
					if (job.id == this.jobs[i].id) {
						this.jobs.splice(i, 1, job);
						this.$forceUpdate();
						break;
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	
	.pt88 {
		padding-top: 88rpx;
	}

	.page-head {
		height: 88rpx;
		background-color: var(--primary);

		.tab_item {
			height: 88rpx;
			line-height: 88rpx;
			text-align: center;
			font-size: 26rpx;
			color: #FFFFFF;

			&.active {
				font-weight: bold;
				font-size: 26rpx;
				color: #FFFFFF;
				position: relative;

				&::after {
					content: '';
					width: 36rpx;
					height: 6rpx;
					background: #FFFFFF;
					border-radius: 4rpx;
					position: absolute;
					bottom: 0;
					left: 50%;
					transform: translateX(-50%);
				}
			}
		}
	}

	.picker_nav {
		width: 690rpx;
		height: 85rpx;
		background: #FFFFFF;
		border-radius: 43rpx;
		margin-top: 30rpx;
		padding: 0 40rpx;
	}

	.item {
		width: 690rpx;
		padding: 30rpx 30rpx 25rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 0rpx 20rpx 0rpx rgba(0, 0, 0, 0.03);
		border-radius: 18rpx;
		margin-top: 20rpx;

		.label {
			padding: 0 8rpx;
			display: inline-block;
			vertical-align: top;
			margin-right: 10rpx;
			margin-top: 10rpx;
			font-size: 22rpx;
			color: #999;
			height: 32rpx;
			line-height: 32rpx;
			background: #F7F7F7;
			border-radius: 8rpx;

		}

		.bt1 {
			font-size: 24rpx;
			color: #FB0F0F;
			width: 108rpx;
			height: 52rpx;
			border-radius: 50rpx;
			border: 1px solid #FB0F0F;
			line-height: calc(52rpx - 2px);
			text-align: center;
		}
	}

	.bt {
		border-top: 1px solid #EEEEEE;
	}

	.bt2 {
		width: 108rpx;
		height: 52rpx;
		border-radius: 50rpx;
		border: 1px solid #696969;
		line-height: calc(52rpx - 2px);
		text-align: center;
		font-size: 24rpx;
		color: #787878;
	}

	.bt4 {
		height: 52rpx;
		line-height: 52rpx;
		font-size: 24rpx;
		color: #787878;
	}

	.bt3 {
		width: 108rpx;
		height: 52rpx;
		border-radius: 50rpx;
		border: 1px solid var(--primary);
		line-height: calc(52rpx - 2px);
		text-align: center;
		font-size: 24rpx;
		color: var(--primary);
	}
</style>